//
//  ToolBoxDetailsViewController.m
//  WisdomZhuHai
//
//  Created by BABLE on 15/5/7.
//  Copyright (c) 2015年 wyd. All rights reserved.
//

#import "ToolBoxDetailsViewController.h"
#import "TransforController.h"

#import "ProtocolManager.h"
#import "ProtocolDefine.h"
#import "Common.h"

#import "SportsDetailViewController.h"
#import "GoodDetailViewController.h"

#import "PBWebViewController.h"
#import "PBSafariActivity.h"

#import "NewEmptyViewController.h"

#define DEFAULT_CELL_HEIGH 101

@interface ToolBoxDetailsViewController ()
@property (strong,nonatomic)NSDictionary *apiData;
@property (strong,nonatomic)NSArray *listData;
@property BOOL isShow;

@property (strong, nonatomic) PBWebViewController *webViewController;
@end

@implementation ToolBoxDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
//    [self downLoadApiData:self.toolId];
}

-(void)downLoadApiData:(NSString *)tId{
    [[ProtocolManager shareInstance]getToolDetailsListWithId:self.toolId andTarget:self selector:@selector(getToolDetailSListCallBack:)];
}
-(void)getToolDetailSListCallBack:(NSDictionary *)dic{
    
    id isEmptyList = [[dic objectForKey:P_V2_TOOLDETAIL] objectForKey:P_V2_TOOLDETAIL_TOOLLIST];
    if ((dic == nil) || [isEmptyList isMemberOfClass:[NSNull class]]) {
        UIView *emptyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
        emptyView.backgroundColor = [UIColor whiteColor];
//        UIImageView *expectation = [[UIImageView alloc] initWithFrame:CGRectMake(emptyView.bounds.size.width/2-20, emptyView.bounds.size.height/2-85, 41, 63)];
//        [expectation setImage:[UIImage imageNamed:@"doPleaseExpectation@2x"]];
//        UILabel *ecText = [[UILabel alloc] initWithFrame:CGRectMake(emptyView.bounds.size.width/2-20, emptyView.bounds.size.height/2-85, emptyView.bounds.size.width, 25)];
//        ecText.text = @"敬请期待";
//        [emptyView addSubview:expectation];
//        [ecText addSubview:emptyView];
        
        NewEmptyViewController *em = [[NewEmptyViewController alloc] init];
        self.isShow = NO;
        
        [self.view addSubview:em.view];
        return;
    }
    
    self.apiData = [Common valueForKey:P_V2_TOOLDETAIL dic:dic];
    self.listData = [self.apiData objectForKey:P_V2_TOOLDETAIL_TOOLLIST];

    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = nil;
    NSDictionary *cellDic = self.listData[indexPath.row];
    
    if((indexPath.row >= self.listData.count) || (cellDic == nil)){
        cell = [tableView dequeueReusableCellWithIdentifier: @"nilCell"];
        if(cell == nil){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: @"nilCell"];
        }
        UIView *nilCellView = [[UIView alloc] init];
        nilCellView.backgroundColor = [UIColor whiteColor];
        [cell addSubview:nilCellView];
        return cell;
    }
    
    cell = [tableView dequeueReusableCellWithIdentifier:@"CommonImageTextCell"];
    if (cell == nil) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"CommonImageTextCell"  owner:self options:nil][0];
    }
    UIImageView *img = (UIImageView *)[cell viewWithTag:98100001];
    UILabel *name = (UILabel *)[cell viewWithTag:98100002];
    UILabel *intro = (UILabel *)[cell viewWithTag:98100003];
    
    [img setImage:[UIImage imageNamed:@"placeholder@2x"]];
    NSString *imgUrl = [Common valueForKey:P_V2_TOOLDETAIL_TOOLLIST_IMAGE dic:cellDic];
    if (imgUrl != nil) {
        [img setImageWithURL:[Common getFullDownloadPathByRelativePath:imgUrl]];
    }
    
    name.text = [Common valueForKey:P_V2_TOOLDETAIL_TOOLLIST_NAME dic:cellDic];
    intro.text = [Common valueForKey:P_V2_TOOLDETAIL_TOOLLIST_INTRO dic:cellDic];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.listData[indexPath.row] == nil) {
        return;
    }
    
    NSDictionary *cellDic = self.listData[indexPath.row];
    NSString *cellType = [Common valueForKey:P_V2_TOOLDETAIL_TOOLLIST_TYPE dic:cellDic];
    NSString *cellLinkId = [Common valueForKey:P_V2_TOOLDETAIL_TOOLLIST_REL_ID dic:cellDic];
    NSString *cellName = [Common valueForKey:P_V2_TOOLDETAIL_TOOLLIST_NAME dic:cellDic];
    
    if ([cellType isEqualToString:P_V2_TOOLDETAIL_TOOLLIST_TYPE_URL]) {//跳网页
        
        NSString *url = [NSString stringWithString:[Common valueForKey:P_V2_TOOLDETAIL_TOOLLIST_URL dic:cellDic]];

//        WebIOS *web = [[WebIOS alloc]initWithFrame:[Common shareInstance].contentFrameNoTabBar url:[Common getUrlByString:url]];
//        web.hidesBottomBarWhenPushed = YES;
//        web.target_id = [cellLinkId intValue];
//        web.navigationItem.title = cellName;
//        [self.navigationController pushViewController:web animated:YES];

        self.webViewController = [[PBWebViewController alloc] init];
        UIView *view_navigationBar = [TransforController shardeInstanceOfNavigationBarAndStatusBarCoverViewWithY:0];
        [self.webViewController.view addSubview:view_navigationBar];
        
        PBSafariActivity *activity = [[PBSafariActivity alloc] init];
        self.webViewController.URL = [NSURL URLWithString:url];
        self.webViewController.applicationActivities = @[activity];
        self.webViewController.excludedActivityTypes = @[UIActivityTypeMail, UIActivityTypeMessage, UIActivityTypePostToWeibo];
    
        self.webViewController.hidesBottomBarWhenPushed = YES;
        self.webViewController.navigationItem.title = cellName;
        [self.navigationCtrl pushViewController:self.webViewController animated:YES];
        
    }else if ([cellType isEqualToString:P_V2_TOOLDETAIL_TOOLLIST_TYPE_L_NEWS]){//跳新闻
        NSString *encryptedParams = [NSString stringWithFormat:@"id=%@",cellLinkId];
        NSMutableString* url = [NSMutableString stringWithFormat:@"%@/%@/%@", OKAPIBASEURL, APINEWRELEASE, R_GETCONTENT];
        [url appendFormat:@"?%@", encryptedParams];
        WebIOS *web  = [[WebIOS alloc]initWithFrame:[Common shareInstance].contentFrameNoTabBar url:[Common getUrlByString:url]];
        web.hidesBottomBarWhenPushed = YES;
        web.type = @"NEWS";
        web.target_id = [cellLinkId intValue];
        web.navigationItem.title = cellName;
        [self.navigationController pushViewController:web animated:YES];
        
    }else if ([cellType isEqualToString:P_V2_TOOLDETAIL_TOOLLIST_TYPE_L_STORE]){//跳商家
        SportsDetailViewController *storeViewController = [[SportsDetailViewController alloc]init];
        storeViewController.target_id = [cellLinkId intValue];
        storeViewController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:storeViewController animated:YES];
        
    }else if ([cellType isEqualToString:P_V2_TOOLDETAIL_TOOLLIST_TYPE_L_GOODS]){//跳商品
        GoodDetailViewController *goodDetailViewController = [[GoodDetailViewController alloc]init];
        goodDetailViewController.goodId = [cellLinkId intValue];
        goodDetailViewController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:goodDetailViewController animated:YES];
    }else if ([cellType isEqualToString:P_V2_TOOLDETAIL_TOOLLIST_TYPE_APP]) {//跳APP
        NSString *appUrl = [Common valueForKey:P_V2_TOOLDETAIL_TOOLLIST_URL dic:cellDic];
        if (appUrl != nil){
            BOOL isJump = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appUrl]];
            if (isJump == NO){
                NSLog(@"YOU SHALL NOT PASS");
                [[Common shareInstance] showAlertViewWithTitle:@"您尚未安装此应用" text:nil dismissTime:1.0f];
            }
        }
    }
    return;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.listData.count;
//    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return DEFAULT_CELL_HEIGH;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
